package com.eduagent.xwqeduagent.service.lmpl;

import cn.hutool.core.util.StrUtil;
import com.eduagent.xwqeduagent.api.CreativePosterGeneration.api.CreativePosterGenerationApi;
import com.eduagent.xwqeduagent.api.CreativePosterGeneration.model.CreatePosterTaskRequest;
import com.eduagent.xwqeduagent.api.CreativePosterGeneration.model.CreatePosterTaskResponse;
import com.eduagent.xwqeduagent.api.CreativePosterGeneration.model.GetPosterTaskResponse;
import com.eduagent.xwqeduagent.exception.BusinessException;
import com.eduagent.xwqeduagent.exception.ErrorCode;
import com.eduagent.xwqeduagent.exception.ThrowUtils;
import com.eduagent.xwqeduagent.model.dto.CreatePosterRequest;
import com.eduagent.xwqeduagent.service.PosterService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 创意海报生成服务实现类
 */
@Slf4j
@Service
public class PosterServiceImpl implements PosterService {

    @Resource
    private CreativePosterGenerationApi creativePosterGenerationApi;

    /**
     * 创建创意海报生成任务
     *
     * @param createPosterRequest 创意海报生成请求
     * @return 创建任务的响应
     */
    @Override
    public CreatePosterTaskResponse createPosterTask(CreatePosterRequest createPosterRequest) {
        // 参数校验
        ThrowUtils.throwIf(createPosterRequest == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        
        // 核心参数校验
        ThrowUtils.throwIf(StrUtil.isBlank(createPosterRequest.getTitle()), 
                ErrorCode.PARAMS_ERROR, "主标题不能为空");
        ThrowUtils.throwIf(StrUtil.isBlank(createPosterRequest.getPromptTextZh()) && StrUtil.isBlank(createPosterRequest.getPromptTextEn()), 
                ErrorCode.PARAMS_ERROR, "中文提示词和英文提示词至少需要设置一个");
        
        // 校验高分辨率模式或高清修复模式的参数
        if ("sr".equals(createPosterRequest.getGenerateMode()) || "hrf".equals(createPosterRequest.getGenerateMode())) {
            ThrowUtils.throwIf(StrUtil.isBlank(createPosterRequest.getAuxiliaryParameters()), 
                    ErrorCode.PARAMS_ERROR, "高分辨率模式或高清修复模式需要提供辅助参数");
        }
        
        // 构建API请求参数
        CreatePosterTaskRequest taskRequest = new CreatePosterTaskRequest();
        taskRequest.setModel("wanx-poster-generation-v1");
        
        // 设置输入参数
        CreatePosterTaskRequest.Input input = new CreatePosterTaskRequest.Input();
        input.setTitle(createPosterRequest.getTitle());
        input.setSubTitle(createPosterRequest.getSubTitle());
        input.setBodyText(createPosterRequest.getBodyText());
        input.setPromptTextZh(createPosterRequest.getPromptTextZh());
        input.setPromptTextEn(createPosterRequest.getPromptTextEn());
        input.setWhRatios(createPosterRequest.getWhRatios());
        input.setLoraName(createPosterRequest.getLoraName());
        input.setLoraWeight(createPosterRequest.getLoraWeight());
        input.setCtrlRatio(createPosterRequest.getCtrlRatio());
        input.setCtrlStep(createPosterRequest.getCtrlStep());
        input.setGenerateMode(createPosterRequest.getGenerateMode());
        input.setGenerateNum(createPosterRequest.getGenerateNum());
        input.setAuxiliaryParameters(createPosterRequest.getAuxiliaryParameters());
        input.setCreativeTitleLayout(createPosterRequest.getCreativeTitleLayout());
        
        taskRequest.setInput(input);
        
        // 打印请求体，方便调试
        log.info("创建创意海报生成任务请求体准备完成");
        
        // 调用API
        return creativePosterGenerationApi.createPosterTask(taskRequest);
    }

    /**
     * 获取创意海报生成任务结果
     *
     * @param taskId 任务ID
     * @return 任务查询结果
     */
    @Override
    public GetPosterTaskResponse getPosterTask(String taskId) {
        // 参数校验
        ThrowUtils.throwIf(StrUtil.isBlank(taskId), ErrorCode.PARAMS_ERROR, "任务ID不能为空");
        
        // 调用API
        return creativePosterGenerationApi.getPosterTask(taskId);
    }
} 